home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gp_mswtx.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  20KB  |  772 lines

  1. /* Copyright (C) 1993  Russell Lang
  2.  
  3. Permission to use, copy, modify, distribute, and sell this
  4. software and its documentation for any purpose is hereby granted
  5. without fee, provided that the above copyright notice and this
  6. permission notice appear in all copies of the software and related
  7. documentation.
  8. */
  9.  
  10. /* gp_mswtx.c */
  11. /*
  12.  * Microsoft Windows 3.n text window for Ghostscript.
  13.  * Original version by Russell Lang
  14.  */
  15.  
  16. #define STRICT
  17. #include "windows_.h"
  18. #include <windowsx.h>
  19. #if WINVER >= 0x030a
  20. #include <commdlg.h>
  21. #include <shellapi.h>
  22. #endif
  23. #include "ctype_.h"
  24. #include "memory_.h"
  25. #include "string_.h"    /* use only far items */
  26. #include <stdlib.h>
  27. #include "dos_.h"
  28.  
  29. #include "gp_mswin.h"
  30. #include "gp_mswtx.h"
  31.  
  32. /* sysmenu */
  33. #define M_COPY_CLIP 1
  34. /* font stuff */
  35. #define TEXTFONTSIZE 9
  36. #define TEXTFONTNAME "Terminal"
  37.  
  38. /* limits */
  39. POINT ScreenMinSize = {16,4};
  40. LRESULT CALLBACK _export WndTextProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  41.  
  42. char szNoMemory[] = "out of memory";
  43. LPSTR szTextClass = "gstext_class";
  44.  
  45. void
  46. TextMessage(void)
  47. {
  48.     MSG msg;
  49.     while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  50.         {
  51.         TranslateMessage(&msg);
  52.         DispatchMessage(&msg);
  53.         }
  54.     return;
  55. }
  56.  
  57.  
  58. void
  59. CreateTextClass(LPTW lptw)
  60. {
  61. WNDCLASS wndclass;
  62. static BOOL init = FALSE;
  63.     if (init)
  64.         return;
  65.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  66.     wndclass.lpfnWndProc = WndTextProc;
  67.     wndclass.cbClsExtra = 0;
  68.     wndclass.cbWndExtra = sizeof(void FAR *);
  69.     wndclass.hInstance = lptw->hInstance;
  70.     if (lptw->hIcon)
  71.         wndclass.hIcon = lptw->hIcon;
  72.     else
  73.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  74.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  75.     wndclass.hbrBackground = GetStockBrush(WHITE_BRUSH);
  76.     wndclass.lpszMenuName = NULL;
  77.     wndclass.lpszClassName = szTextClass;
  78.     RegisterClass(&wndclass);
  79.     init = TRUE;
  80. }
  81.  
  82.  
  83. /* make text window */
  84. int
  85. TextInit(LPTW lptw)
  86. {
  87.     HMENU sysmenu;
  88.     HGLOBAL hglobal;
  89.     
  90.     if (!lptw->hPrevInstance)
  91.         CreateTextClass(lptw);
  92.  
  93.     if (lptw->KeyBufSize == 0)
  94.         lptw->KeyBufSize = 256;
  95.  
  96.     if (lptw->ScreenSize.x < ScreenMinSize.x)
  97.         lptw->ScreenSize.x = ScreenMinSize.x;
  98.     if (lptw->ScreenSize.y < ScreenMinSize.y)
  99.         lptw->ScreenSize.y = ScreenMinSize.y;
  100.  
  101.     lptw->CursorPos.x = lptw->CursorPos.y = 0;
  102.     lptw->bFocus = FALSE;
  103.     lptw->bGetCh = FALSE;
  104.     lptw->CaretHeight = 0;
  105.     if (!lptw->nCmdShow)
  106.         lptw->nCmdShow = SW_SHOWNORMAL;
  107.  
  108.     hglobal = GlobalAlloc(GHND, lptw->ScreenSize.x * lptw->ScreenSize.y);
  109.     lptw->ScreenBuffer = (BYTE FAR *)GlobalLock(hglobal);
  110.     if (lptw->ScreenBuffer == (BYTE FAR *)NULL) {
  111.         MessageBox((HWND)NULL,szNoMemory,(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  112.         return(1);
  113.     }
  114.     _fmemset(lptw->ScreenBuffer, ' ', lptw->ScreenSize.x * lptw->ScreenSize.y);
  115.     hglobal = GlobalAlloc(LHND, lptw->KeyBufSize);
  116.     lptw->KeyBuf = (BYTE FAR *)GlobalLock(hglobal);
  117.     if (lptw->KeyBuf == (BYTE FAR *)NULL) {
  118.         MessageBox((HWND)NULL,szNoMemory,(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  119.         return(1);
  120.     }
  121.     lptw->KeyBufIn = lptw->KeyBufOut = lptw->KeyBuf;
  122.  
  123.     lptw->hWndText = CreateWindow(szTextClass, lptw->Title,
  124.           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  125.           CW_USEDEFAULT, CW_USEDEFAULT,
  126.           CW_USEDEFAULT, CW_USEDEFAULT,
  127.           NULL, NULL, lptw->hInstance, lptw);
  128.     if (lptw->hWndText == (HWND)NULL) {
  129.         MessageBox((HWND)NULL,"Couldn't open text window",(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  130.         return(1);
  131.     }
  132.     ShowWindow(lptw->hWndText, lptw->nCmdShow);
  133.     sysmenu = GetSystemMenu(lptw->hWndText,0);    /* get the sysmenu */
  134.     AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  135.     AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  136.  
  137.     return(0);
  138. }
  139.  
  140. /* close a text window */
  141. void
  142. TextClose(LPTW lptw)
  143. {
  144.     HGLOBAL hglobal;
  145.  
  146.     /* close window */
  147.     if (lptw->hWndText)
  148.         DestroyWindow(lptw->hWndText);
  149.     TextMessage();
  150.  
  151.     hglobal = (HGLOBAL)GlobalHandle( SELECTOROF(lptw->ScreenBuffer) );
  152.     if (hglobal) {
  153.         GlobalUnlock(hglobal);
  154.         GlobalFree(hglobal);
  155.     }
  156.     hglobal = (HGLOBAL)GlobalHandle( SELECTOROF(lptw->KeyBuf) );
  157.     if (hglobal) {
  158.         GlobalUnlock(hglobal);
  159.         GlobalFree(hglobal);
  160.     }
  161.     lptw->hWndText = (HWND)NULL;
  162. }
  163.     
  164.  
  165. /* Bring Cursor into text window */
  166. void
  167. TextToCursor(LPTW lptw)
  168. {
  169. int nXinc=0;
  170. int nYinc=0;
  171. int cxCursor;
  172. int cyCursor;
  173.     cyCursor = lptw->CursorPos.y * lptw->CharSize.y;
  174.     if ( (cyCursor + lptw->CharSize.y > lptw->ScrollPos.y + lptw->ClientSize.y) 
  175.       || (cyCursor < lptw->ScrollPos.y) ) {
  176.         nYinc = max(0, cyCursor + lptw->CharSize.y - lptw->ClientSize.y) - lptw->ScrollPos.y;
  177.         nYinc = min(nYinc, lptw->ScrollMax.y - lptw->ScrollPos.y);
  178.     }
  179.     cxCursor = lptw->CursorPos.x * lptw->CharSize.x;
  180.     if ( (cxCursor + lptw->CharSize.x > lptw->ScrollPos.x + lptw->ClientSize.x)
  181.       || (cxCursor < lptw->ScrollPos.x) ) {
  182.         nXinc = max(0, cxCursor + lptw->CharSize.x - lptw->ClientSize.x/2) - lptw->ScrollPos.x;
  183.         nXinc = min(nXinc, lptw->ScrollMax.x - lptw->ScrollPos.x);
  184.     }
  185.     if (nYinc || nXinc) {
  186.         lptw->ScrollPos.y += nYinc;
  187.         lptw->ScrollPos.x += nXinc;
  188.         ScrollWindow(lptw->hWndText,-nXinc,-nYinc,NULL,NULL);
  189.         SetScrollPos(lptw->hWndText,SB_VERT,lptw->ScrollPos.y,TRUE);
  190.         SetScrollPos(lptw->hWndText,SB_HORZ,lptw->ScrollPos.x,TRUE);
  191.         UpdateWindow(lptw->hWndText);
  192.     }
  193. }
  194.  
  195. void
  196. NewLine(LPTW lptw)
  197. {
  198.     lptw->CursorPos.x = 0;
  199.     lptw->CursorPos.y++;
  200.     if (lptw->CursorPos.y >= lptw->ScreenSize.y) {
  201.         int i =  lptw->ScreenSize.x * (lptw->ScreenSize.y - 1);
  202.         _fmemmove(lptw->ScreenBuffer, lptw->ScreenBuffer+lptw->ScreenSize.x, i);
  203.         _fmemset(lptw->ScreenBuffer + i, ' ', lptw->ScreenSize.x);
  204.         lptw->CursorPos.y--;
  205.         ScrollWindow(lptw->hWndText,0,-lptw->CharSize.y,NULL,NULL);
  206.         UpdateWindow(lptw->hWndText);
  207.     }
  208.     if (lptw->CursorFlag)
  209.         TextToCursor(lptw);
  210.     TextMessage();
  211. }
  212.  
  213. /* Update count characters in window at cursor position */
  214. /* Updates cursor position */
  215. void
  216. UpdateText(LPTW lptw, int count)
  217. {
  218. HDC hdc;
  219. int xpos, ypos;
  220.     xpos = lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x;
  221.     ypos = lptw->CursorPos.y*lptw->CharSize.y - lptw->ScrollPos.y;
  222.     hdc = GetDC(lptw->hWndText);
  223.     SelectFont(hdc, lptw->hfont);
  224.     TextOut(hdc,xpos,ypos,
  225.         (LPSTR)(lptw->ScreenBuffer + lptw->CursorPos.y*lptw->ScreenSize.x + 
  226.         lptw->CursorPos.x), count);
  227.     (void)ReleaseDC(lptw->hWndText,hdc);
  228.     lptw->CursorPos.x += count;
  229.     if (lptw->CursorPos.x >= lptw->ScreenSize.x)
  230.         NewLine(lptw);
  231. }
  232.  
  233. int
  234. TextPutCh(LPTW lptw, BYTE ch)
  235. {
  236. int pos;
  237.     switch(ch) {
  238.         case '\r':
  239.             lptw->CursorPos.x = 0;
  240.             if (lptw->CursorFlag)
  241.                 TextToCursor(lptw);
  242.             break;
  243.         case '\n':
  244.             NewLine(lptw);
  245.             break;
  246.         case 7:
  247.             MessageBeep(-1);
  248.             if (lptw->CursorFlag)
  249.                 TextToCursor(lptw);
  250.             break;
  251.         case '\t':
  252.             {
  253.             int n;
  254.                 for ( n = 8 - (lptw->CursorPos.x % 8); n>0; n-- )
  255.                     TextPutCh(lptw, ' ');
  256.             }
  257.             break;
  258.         case 0x08:
  259.         case 0x7f:
  260.             lptw->CursorPos.x--;
  261.             if (lptw->CursorPos.x < 0) {
  262.                 lptw->CursorPos.x = lptw->ScreenSize.x - 1;
  263.                 lptw->CursorPos.y--;
  264.             }
  265.             if (lptw->CursorPos.y < 0)
  266.                 lptw->CursorPos.y = 0;
  267.             break;
  268.         default:
  269.             pos = lptw->CursorPos.y*lptw->ScreenSize.x + lptw->CursorPos.x;
  270.             lptw->ScreenBuffer[pos] = ch;
  271.             UpdateText(lptw, 1);
  272.     }
  273.     return ch;
  274. }
  275.  
  276. void 
  277. TextWriteBuf(LPTW lptw, LPSTR str, int cnt)
  278. {
  279. BYTE FAR *p;
  280. int count, limit;
  281.     while (cnt>0) {
  282.     p = lptw->ScreenBuffer + lptw->CursorPos.y*lptw->ScreenSize.x + lptw->CursorPos.x;
  283.     limit = lptw->ScreenSize.x - lptw->CursorPos.x;
  284.     for (count=0; (count < limit) && (cnt>0) && (isprint(*str) || *str=='\t'); count++) {
  285.         if (*str=='\t') {
  286.         int n;
  287.         for ( n = 8 - ((lptw->CursorPos.x+count) % 8); (count < limit) & (n>0); n--, count++ ) {
  288.             *p++ = ' ';
  289.         }
  290.         str++;
  291.         count--;
  292.            }
  293.         else {
  294.         *p++ = *str++;
  295.         }
  296.         cnt--;
  297.     }
  298.     if (count>0) {
  299.         UpdateText(lptw, count);
  300.     }
  301.     if (cnt > 0) {
  302.         if (*str=='\n') {
  303.         NewLine(lptw);
  304.         str++;
  305.         cnt--;
  306.         }
  307.         else if (!isprint(*str) && *str!='\t') {
  308.         TextPutCh(lptw, *str++);
  309.         cnt--;
  310.         }
  311.     }
  312.     }
  313. }
  314.  
  315.  
  316. /* TRUE if key hit, FALSE if no key */
  317. int
  318. TextKBHit(LPTW lptw)
  319. {
  320.     return (lptw->KeyBufIn != lptw->KeyBufOut);
  321. }
  322.  
  323. /* get character from keyboard, no echo */
  324. /* need to add extended codes */
  325. int
  326. TextGetCh(LPTW lptw)
  327. {
  328.     int ch;
  329.     TextToCursor(lptw);
  330.     lptw->bGetCh = TRUE;
  331.     if (lptw->bFocus) {
  332.         SetCaretPos(lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x,
  333.             lptw->CursorPos.y*lptw->CharSize.y + lptw->CharAscent 
  334.             - lptw->CaretHeight - lptw->ScrollPos.y);
  335.         ShowCaret(lptw->hWndText);
  336.     }
  337.     do {
  338.         TextMessage();
  339.     } while (!TextKBHit(lptw));
  340.     ch = *lptw->KeyBufOut++;
  341.     if (ch=='\r')
  342.         ch = '\n';
  343.     if (lptw->KeyBufOut - lptw->KeyBuf >= lptw->KeyBufSize)
  344.         lptw->KeyBufOut = lptw->KeyBuf;    /* wrap around */
  345.     if (lptw->bFocus)
  346.         HideCaret(lptw->hWndText);
  347.     lptw->bGetCh = FALSE;
  348.     return ch;
  349. }
  350.  
  351. #if WINVER >= 0x030a
  352. /* Windows 3.1 drag-drop feature */
  353. char szFile[80];
  354. void
  355. DragFunc(LPTW lptw, HDROP hdrop)
  356. {
  357.     int i, cFiles;
  358.     LPSTR p;
  359.     if ( (lptw->DragPre==(LPSTR)NULL) || (lptw->DragPost==(LPSTR)NULL) )
  360.         return;
  361.     cFiles = DragQueryFile(hdrop, 0xffff, (LPSTR)NULL, 0);
  362.     for (i=0; i<cFiles; i++) {
  363.         DragQueryFile(hdrop, i, szFile, 80);
  364.         for (p=lptw->DragPre; *p; p++)
  365.             SendMessage(lptw->hWndText,WM_CHAR,*p,1L);
  366.         for (p=szFile; *p; p++) {
  367.             if (*p == '\\')
  368.             SendMessage(lptw->hWndText,WM_CHAR,'/',1L);
  369.             else 
  370.             SendMessage(lptw->hWndText,WM_CHAR,*p,1L);
  371.         }
  372.         for (p=lptw->DragPost; *p; p++)
  373.             SendMessage(lptw->hWndText,WM_CHAR,*p,1L);
  374.     }
  375.     DragFinish(hdrop);
  376. }
  377. #endif
  378.  
  379.  
  380. void
  381. TextMakeFont(LPTW lptw)
  382. {
  383.     LOGFONT lf;
  384.     TEXTMETRIC tm;
  385.     LPSTR p;
  386.     HDC hdc;
  387.     
  388.     if ((lptw->fontname[0]=='\0') || (lptw->fontsize==0)) {
  389.         _fstrcpy(lptw->fontname,TEXTFONTNAME);
  390.         lptw->fontsize = TEXTFONTSIZE;
  391.     }
  392.  
  393.     hdc = GetDC(lptw->hWndText);
  394.     _fmemset(&lf, 0, sizeof(LOGFONT));
  395.     _fstrncpy(lf.lfFaceName,lptw->fontname,LF_FACESIZE);
  396.     lf.lfHeight = -MulDiv(lptw->fontsize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
  397.     lf.lfPitchAndFamily = FIXED_PITCH;
  398.     lf.lfCharSet = DEFAULT_CHARSET;
  399.     if ( (p = _fstrstr(lptw->fontname," Italic")) != (LPSTR)NULL ) {
  400.         lf.lfFaceName[ (unsigned int)(p-lptw->fontname) ] = '\0';
  401.         lf.lfItalic = TRUE;
  402.     }
  403.     if ( (p = _fstrstr(lptw->fontname," Bold")) != (LPSTR)NULL ) {
  404.         lf.lfFaceName[ (unsigned int)(p-lptw->fontname) ] = '\0';
  405.         lf.lfWeight = FW_BOLD;
  406.     }
  407.     if (lptw->hfont != 0)
  408.         DeleteFont(lptw->hfont);
  409.     lptw->hfont = CreateFontIndirect((LOGFONT FAR *)&lf);
  410.     /* get text size */
  411.     SelectFont(hdc, lptw->hfont);
  412.     GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
  413.     lptw->CharSize.y = tm.tmHeight;
  414.     lptw->CharSize.x = tm.tmAveCharWidth;
  415.     lptw->CharAscent = tm.tmAscent;
  416.     if (lptw->bFocus)
  417.         CreateCaret(lptw->hWndText, 0, lptw->CharSize.x, 2+lptw->CaretHeight);
  418.     ReleaseDC(lptw->hWndText, hdc);
  419.     return;
  420. }
  421.  
  422. void
  423. TextCopyClip(LPTW lptw)
  424. {
  425.     int size, count;
  426.     HGLOBAL hGMem;
  427.     LPSTR cbuf, cp;
  428.     TEXTMETRIC tm;
  429.     UINT type;
  430.     HDC hdc;
  431.     int i;
  432.  
  433.     size = lptw->ScreenSize.y * (lptw->ScreenSize.x + 2) + 1;
  434.     hGMem = GlobalAlloc(GHND | GMEM_SHARE, (DWORD)size);
  435.     cbuf = cp = (LPSTR)GlobalLock(hGMem);
  436.     if (cp == (LPSTR)NULL)
  437.         return;
  438.     
  439.     for (i=0; i<lptw->ScreenSize.y; i++) {
  440.         count = lptw->ScreenSize.x;
  441.         _fmemcpy(cp, lptw->ScreenBuffer + lptw->ScreenSize.x*i, count);
  442.         /* remove trailing spaces */
  443.         for (count=count-1; count>=0; count--) {
  444.             if (cp[count]!=' ')
  445.                 break;
  446.             cp[count] = '\0';
  447.         }
  448.         cp[++count] = '\r';
  449.         cp[++count] = '\n';
  450.         cp[++count] = '\0';
  451.         cp += count;
  452.     }
  453.     size = _fstrlen(cbuf) + 1;
  454.     GlobalUnlock(hGMem);
  455.     hGMem = GlobalReAlloc(hGMem, (DWORD)size, GHND | GMEM_SHARE);
  456.     /* find out what type to put into clipboard */
  457.     hdc = GetDC(lptw->hWndText);
  458.     SelectFont(hdc, lptw->hfont);
  459.     GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
  460.     if (tm.tmCharSet == OEM_CHARSET)
  461.         type = CF_OEMTEXT;
  462.     else
  463.         type = CF_TEXT;
  464.     ReleaseDC(lptw->hWndText, hdc);
  465.     /* give buffer to clipboard */
  466.     OpenClipboard(lptw->hWndText);
  467.     EmptyClipboard();
  468.     SetClipboardData(type, hGMem);
  469.     CloseClipboard();
  470. }
  471.  
  472.  
  473. /* text window */
  474. LRESULT CALLBACK _export
  475. WndTextProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  476. {
  477.     HDC hdc;
  478.     PAINTSTRUCT ps;
  479.     RECT rect;
  480.     int nYinc, nXinc;
  481.     LPTW lptw;
  482.  
  483.     lptw = (LPTW)GetWindowLong(hwnd, 0);
  484.  
  485.     switch(message) {
  486.         case WM_GSVIEW:
  487.             if (wParam != PIPE_DATA)
  488.                 return 0;
  489.             if (pipe_count) {
  490.                 MessageBox(hwnd, "pipe overflow", szAppName, MB_OK);
  491.                 pipe_count = 0;
  492.                 if (pipe_lpbyte != (LPBYTE)NULL)
  493.                     GlobalUnlock(pipe_hglobal);
  494.                 if (pipe_hglobal != (HGLOBAL)NULL)
  495.                     GlobalFree(pipe_hglobal);
  496.             }
  497.             pipe_count = HIWORD(lParam);
  498.             pipe_hglobal = (HGLOBAL)LOWORD(lParam);
  499.             return 0;
  500.         case WM_SYSCOMMAND:
  501.             switch(LOWORD(wParam)) {
  502.                 case M_COPY_CLIP:
  503.                     TextCopyClip(lptw);
  504.                     return 0;
  505.             }
  506.             break;
  507.         case WM_SETFOCUS: 
  508.             lptw->bFocus = TRUE;
  509.             CreateCaret(hwnd, 0, lptw->CharSize.x, 2+lptw->CaretHeight);
  510.             SetCaretPos(lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x,
  511.                 lptw->CursorPos.y*lptw->CharSize.y + lptw->CharAscent
  512.                  - lptw->CaretHeight - lptw->ScrollPos.y);
  513.             if (lptw->bGetCh)
  514.                 ShowCaret(hwnd);
  515.             break;
  516.         case WM_KILLFOCUS: 
  517.             DestroyCaret();
  518.             lptw->bFocus = FALSE;
  519.             break;
  520.         case WM_SIZE:
  521.             lptw->ClientSize.y = HIWORD(lParam);
  522.             lptw->ClientSize.x = LOWORD(lParam);
  523.  
  524.             lptw->ScrollMax.y = max(0, lptw->CharSize.y*lptw->ScreenSize.y - lptw->ClientSize.y);
  525.             lptw->ScrollPos.y = min(lptw->ScrollPos.y, lptw->ScrollMax.y);
  526.  
  527.             SetScrollRange(hwnd, SB_VERT, 0, lptw->ScrollMax.y, FALSE);
  528.             SetScrollPos(hwnd, SB_VERT, lptw->ScrollPos.y, TRUE);
  529.  
  530.             lptw->ScrollMax.x = max(0, lptw->CharSize.x*lptw->ScreenSize.x - lptw->ClientSize.x);
  531.             lptw->ScrollPos.x = min(lptw->ScrollPos.x, lptw->ScrollMax.x);
  532.  
  533.             SetScrollRange(hwnd, SB_HORZ, 0, lptw->ScrollMax.x, FALSE);
  534.             SetScrollPos(hwnd, SB_HORZ, lptw->ScrollPos.x, TRUE);
  535.  
  536.             if (lptw->bFocus && lptw->bGetCh) {
  537.                 SetCaretPos(lptw->CursorPos.x*lptw->CharSize.x - lptw->ScrollPos.x,
  538.                     lptw->CursorPos.y*lptw->CharSize.y + lptw->CharAscent 
  539.                     - lptw->CaretHeight - lptw->ScrollPos.y);
  540.                 ShowCaret(hwnd);
  541.             }
  542.             return(0);
  543.         case WM_VSCROLL:
  544.             switch(LOWORD(wParam)) {
  545.                 case SB_TOP:
  546.                     nYinc = -lptw->ScrollPos.y;
  547.                     break;
  548.                 case SB_BOTTOM:
  549.                     nYinc = lptw->ScrollMax.y - lptw->ScrollPos.y;
  550.                     break;
  551.                 case SB_LINEUP:
  552.                     nYinc = -lptw->CharSize.y;
  553.                     break;
  554.                 case SB_LINEDOWN:
  555.                     nYinc = lptw->CharSize.y;
  556.                     break;
  557.                 case SB_PAGEUP:
  558.                     nYinc = min(-1,-lptw->ClientSize.y);
  559.                     break;
  560.                 case SB_PAGEDOWN:
  561.                     nYinc = max(1,lptw->ClientSize.y);
  562.                     break;
  563.                 case SB_THUMBPOSITION:
  564.                     nYinc = LOWORD(lParam) - lptw->ScrollPos.y;
  565.                     break;
  566.                 default:
  567.                     nYinc = 0;
  568.                 }
  569.             if ( (nYinc = max(-lptw->ScrollPos.y, 
  570.                 min(nYinc, lptw->ScrollMax.y - lptw->ScrollPos.y)))
  571.                 != 0 ) {
  572.                 lptw->ScrollPos.y += nYinc;
  573.                 ScrollWindow(hwnd,0,-nYinc,NULL,NULL);
  574.                 SetScrollPos(hwnd,SB_VERT,lptw->ScrollPos.y,TRUE);
  575.                 UpdateWindow(hwnd);
  576.             }
  577.             return(0);
  578.         case WM_HSCROLL:
  579.             switch(LOWORD(wParam)) {
  580.                 case SB_LINEUP:
  581.                     nXinc = -lptw->CharSize.x;
  582.                     break;
  583.                 case SB_LINEDOWN:
  584.                     nXinc = lptw->CharSize.x;
  585.                     break;
  586.                 case SB_PAGEUP:
  587.                     nXinc = min(-1,-lptw->ClientSize.x);
  588.                     break;
  589.                 case SB_PAGEDOWN:
  590.                     nXinc = max(1,lptw->ClientSize.x);
  591.                     break;
  592.                 case SB_THUMBPOSITION:
  593.                     nXinc = LOWORD(lParam) - lptw->ScrollPos.x;
  594.                     break;
  595.                 default:
  596.                     nXinc = 0;
  597.                 }
  598.             if ( (nXinc = max(-lptw->ScrollPos.x, 
  599.                 min(nXinc, lptw->ScrollMax.x - lptw->ScrollPos.x)))
  600.                 != 0 ) {
  601.                 lptw->ScrollPos.x += nXinc;
  602.                 ScrollWindow(hwnd,-nXinc,0,NULL,NULL);
  603.                 SetScrollPos(hwnd,SB_HORZ,lptw->ScrollPos.x,TRUE);
  604.                 UpdateWindow(hwnd);
  605.             }
  606.             return(0);
  607.         case WM_KEYDOWN:
  608.             if (GetKeyState(VK_SHIFT) < 0) {
  609.               switch(wParam) {
  610.                 case VK_HOME:
  611.                     SendMessage(hwnd, WM_VSCROLL, SB_TOP, (LPARAM)0);
  612.                     break;
  613.                 case VK_END:
  614.                     SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)0);
  615.                     break;
  616.                 case VK_PRIOR:
  617.                     SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, (LPARAM)0);
  618.                     break;
  619.                 case VK_NEXT:
  620.                     SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, (LPARAM)0);
  621.                     break;
  622.                 case VK_UP:
  623.                     SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, (LPARAM)0);
  624.                     break;
  625.                 case VK_DOWN:
  626.                     SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, (LPARAM)0);
  627.                     break;
  628.                 case VK_LEFT:
  629.                     SendMessage(hwnd, WM_HSCROLL, SB_LINEUP, (LPARAM)0);
  630.                     break;
  631.                 case VK_RIGHT:
  632.                     SendMessage(hwnd, WM_HSCROLL, SB_LINEDOWN, (LPARAM)0);
  633.                     break;
  634.               }
  635.             }
  636.             else {
  637.               switch(wParam) {
  638.                 case VK_HOME:
  639.                 case VK_END:
  640.                 case VK_PRIOR:
  641.                 case VK_NEXT:
  642.                 case VK_UP:
  643.                 case VK_DOWN:
  644.                 case VK_LEFT:
  645.                 case VK_RIGHT:
  646.                 case VK_DELETE:
  647.                 { /* store key in circular buffer */
  648.                 long count;
  649.                     count = lptw->KeyBufIn - lptw->KeyBufOut;
  650.                     if (count < 0) count += lptw->KeyBufSize;
  651.                     if (count < lptw->KeyBufSize-2) {
  652.                         *lptw->KeyBufIn++ = 0;
  653.                         if (lptw->KeyBufIn - lptw->KeyBuf >= lptw->KeyBufSize)
  654.                             lptw->KeyBufIn = lptw->KeyBuf;    /* wrap around */
  655.                         *lptw->KeyBufIn++ = HIWORD(lParam) & 0xff;
  656.                         if (lptw->KeyBufIn - lptw->KeyBuf >= lptw->KeyBufSize)
  657.                             lptw->KeyBufIn = lptw->KeyBuf;    /* wrap around */
  658.                     }
  659.                 }
  660.               }
  661.             }
  662.             break;
  663.         case WM_CHAR:
  664.             { /* store key in circular buffer */
  665.             long count;
  666.                 count = lptw->KeyBufIn - lptw->KeyBufOut;
  667.                 if (count < 0) count += lptw->KeyBufSize;
  668.                 if (count < lptw->KeyBufSize-1) {
  669.                     *lptw->KeyBufIn++ = wParam;
  670.                     if (lptw->KeyBufIn - lptw->KeyBuf >= lptw->KeyBufSize)
  671.                         lptw->KeyBufIn = lptw->KeyBuf;    /* wrap around */
  672.                 }
  673.             }
  674.             return(0);
  675.         case WM_PAINT:
  676.             {
  677.             POINT source, width, dest;
  678.             hdc = BeginPaint(hwnd, &ps);
  679.             SelectFont(hdc, lptw->hfont);
  680.             SetMapMode(hdc, MM_TEXT);
  681.             SetBkMode(hdc,OPAQUE);
  682.             GetClientRect(hwnd, &rect);
  683.             source.x = (rect.left + lptw->ScrollPos.x) / lptw->CharSize.x;        /* source */
  684.             source.y = (rect.top + lptw->ScrollPos.y) / lptw->CharSize.y;
  685.             dest.x = source.x * lptw->CharSize.x - lptw->ScrollPos.x;                 /* destination */
  686.             dest.y = source.y * lptw->CharSize.y - lptw->ScrollPos.y;
  687.             width.x = ((rect.right  + lptw->ScrollPos.x + lptw->CharSize.x - 1) / lptw->CharSize.x) - source.x; /* width */
  688.             width.y = ((rect.bottom + lptw->ScrollPos.y + lptw->CharSize.y - 1) / lptw->CharSize.y) - source.y;
  689.             if (source.x < 0)
  690.                 source.x = 0;
  691.             if (source.y < 0)
  692.                 source.y = 0;
  693.             if (source.x+width.x > lptw->ScreenSize.x)
  694.                 width.x = lptw->ScreenSize.x - source.x;
  695.             if (source.y+width.y > lptw->ScreenSize.y)
  696.                 width.y = lptw->ScreenSize.y - source.y;
  697.             /* for each line */
  698.             while (width.y>0) {
  699.                 TextOut(hdc,dest.x,dest.y,
  700.                     (LPSTR)(lptw->ScreenBuffer + source.y*lptw->ScreenSize.x + source.x),
  701.                     width.x);
  702.                 dest.y += lptw->CharSize.y;
  703.                 source.y++;
  704.                 width.y--;
  705.             }
  706.             EndPaint(hwnd, &ps);
  707.             return 0;
  708.             }
  709. #if WINVER >= 0x030a
  710.         case WM_DROPFILES:
  711.             {
  712.             WORD version = LOWORD(GetVersion());
  713.             if ((LOBYTE(version)*100 + HIBYTE(version)) >= 310)
  714.                 DragFunc(lptw, (HDROP)wParam);
  715.             }
  716.             break;
  717. #endif
  718.         case WM_CREATE:
  719.             {
  720.             RECT crect, wrect;
  721.             TEXTMETRIC tm;
  722.             lptw = ((CREATESTRUCT FAR *)lParam)->lpCreateParams;
  723.             SetWindowLong(hwnd, 0, (LONG)lptw);
  724.             lptw->hWndText = hwnd;
  725.             /* get character size */
  726.             TextMakeFont(lptw);
  727.             hdc = GetDC(hwnd);
  728.             SelectFont(hdc, lptw->hfont);
  729.             GetTextMetrics(hdc,(LPTEXTMETRIC)&tm);
  730.             lptw->CharSize.y = tm.tmHeight;
  731.             lptw->CharSize.x = tm.tmAveCharWidth;
  732.             lptw->CharAscent = tm.tmAscent;
  733.             ReleaseDC(hwnd,hdc);
  734.             GetClientRect(hwnd, &crect);
  735.             if ( (lptw->CharSize.y*lptw->ScreenSize.y < crect.bottom)
  736.               || (lptw->CharSize.x*lptw->ScreenSize.x < crect.right) ) {
  737.                 /* shrink size */
  738.                 GetWindowRect(lptw->hWndText,&wrect);
  739.                 MoveWindow(lptw->hWndText, wrect.left, wrect.top,
  740.                  wrect.right-wrect.left + (lptw->CharSize.x*lptw->ScreenSize.x - crect.right),
  741.                  wrect.bottom-wrect.top + (lptw->CharSize.y*lptw->ScreenSize.y - crect.bottom),
  742.                  TRUE);
  743.             }
  744. #if WINVER >= 0x030a
  745.             {
  746.             WORD version = LOWORD(GetVersion());
  747.             if ((LOBYTE(version)*100 + HIBYTE(version)) >= 310)
  748.                 if ( (lptw->DragPre!=(LPSTR)NULL) && (lptw->DragPost!=(LPSTR)NULL) )
  749.                     DragAcceptFiles(hwnd, TRUE);
  750.             }
  751. #endif
  752.             }
  753.             break;
  754.         case WM_CLOSE:
  755.             if (lptw->shutdown)
  756.                 (*(lptw->shutdown))();
  757.             break;
  758.         case WM_DESTROY:
  759. #if WINVER >= 0x030a
  760.             {
  761.             WORD version = LOWORD(GetVersion());
  762.             if ((LOBYTE(version)*100 + HIBYTE(version)) >= 310)
  763.                 DragAcceptFiles(hwnd, FALSE);
  764.             }
  765. #endif
  766.             if (lptw->hfont != 0)
  767.                 DeleteFont(lptw->hfont);
  768.             break;
  769.     }
  770.     return DefWindowProc(hwnd, message, wParam, lParam);
  771. }
  772.